package com.zsule.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.zsule.entity.PageEntity;
import com.zsule.entity.ShopAndGoods;
import com.zsule.frameworks.mongodb.service.MongoAbstractService;
import com.zsule.service.IShopGoodsService;

@Service("shopAndGoodsService")
public class ShopGoodsServiceImpl extends MongoAbstractService<ShopAndGoods> implements IShopGoodsService {

	@Override
	public void insertShopAndGoods(ShopAndGoods sad) {
		insert(sad);
	}

	@Override
	public List<ShopAndGoods> getShopperGoods(String userId, ShopAndGoods sGoods, PageEntity page) {
		Query query = new Query(Criteria.where("userId").is(userId));
		String goodsName = sGoods.getGoodsName();
		String goodsCode = sGoods.getGoodsCode();
		String goodsManufacturer = sGoods.getManufacturer();
		String typeCode = sGoods.getTypeCode();
		Integer status = sGoods.getStatus();
		if (!"".equals(goodsName) && goodsName != null) {
			query.addCriteria(Criteria.where("goodsName").regex(Pattern.compile("^.*" + goodsName + ".*$", Pattern.CASE_INSENSITIVE)));
		}
		if (!"".equals(goodsCode) && goodsCode != null) {
			query.addCriteria(Criteria.where("goodsCode").regex(Pattern.compile("^" + goodsCode + ".*$", Pattern.CASE_INSENSITIVE)));
		}
		if (!"".equals(goodsManufacturer) && goodsManufacturer != null) {
			query.addCriteria(Criteria.where("manufacturer").regex(Pattern.compile("^.*" + goodsManufacturer + ".*$", Pattern.CASE_INSENSITIVE)));
		}
		if (!"".equals(typeCode) && typeCode != null) {
			List<String> codeList = getTypesByPath(typeCode);
			query.addCriteria(Criteria.where("typeCode").in(codeList));
		}
		if (status != null && status != 0) {
			query.addCriteria(Criteria.where("status").is(status));
		}
		int count = (int) this.getShopperGoodsCount(query);
		page.setTotalResultSize(count);
		List<Order> orders = new ArrayList<Order>();
		orders.add(new Order(Direction.ASC, "code"));
		query.with(new Sort(orders));
		query.skip(page.getStartRow()).limit(page.getPageSize());
		System.out.println(query.getQueryObject().toString());
		return findList(query);
	}

	private List<String> getTypesByPath(String typeCode) {
		Query query = new Query();
		query.addCriteria(Criteria.where("path").regex(Pattern.compile("^.*:" + typeCode + ":.*$", Pattern.CASE_INSENSITIVE)));
		// List<String> s = goodTypeDao.getMongoTemplate().getCollection("e_good_type").distinct("code",query.getQueryObject());
		// System.out.println(s);
		// System.out.println("______");
		// return goodTypeDao.findList(Criteria.where("path").regex(
		// Pattern.compile("^.*:" + typeCode + ":.*$",
		// Pattern.CASE_INSENSITIVE)), GoodType.class);
		return getDao().getMongoTemplate().getCollection("e_good_type").distinct("code", query.getQueryObject());

	}

	@Override
	public long getShopperGoodsCount(Query query) {
		return size(query);
	}

	@Override
	public void changeOutPrice(ShopAndGoods sad) {
		updateFirst(new Query().addCriteria(Criteria.where("id").is(sad.getId())), Update.update("outPrice", sad.getOutPrice()));
	}

	@Override
	public void batchInSale(String[] ids) {
		updateAll(new Query().addCriteria(Criteria.where("id").in(ids)), Update.update("status", 1));
	}

	@Override
	public void batchOutSale(String[] ids) {
		updateAll(new Query().addCriteria(Criteria.where("id").in(ids)), Update.update("status", 2));
	}

	@Override
	public List<ShopAndGoods> getGoodsByIds(String[] ids) {
		return findList(Criteria.where("id").in(ids));
	}

}
